//
//  BSSTeacherElegantDemeanourHomeViewController.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/15.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSTeacherElegantDemeanourHomeViewController.h"
#import "LCDMultiFunctionSegmentedView.h"
#import "BSSTeacherElegantDemeanourListViewController.h"
#import "BSSAddTeacherHomeViewController.h"
@interface BSSTeacherElegantDemeanourHomeViewController ()<UIScrollViewDelegate>
@property (nonatomic ,strong) LCDMultiFunctionSegmentedView *spellSegmentedView;
@property (nonatomic ,strong) UIScrollView *scrollView;

@end

@implementation BSSTeacherElegantDemeanourHomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatUI];
}
- (void)creatUI{
   
    _spellSegmentedView = [[LCDMultiFunctionSegmentedView alloc]initWithOriginY:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, 40) left:15 right:15 headTitle:@"" headTitleFont:fontCount(15) headTitleColor:@"333333" Titles:@[@"已上架",@"已下架"] titleFont:fontCount(16) titleColor:@"333333" titleSelectedColor:@"333333" isUniform:YES maxUniformCount:4 isVerticalLine:NO verticallineColor:@"#F65758" verticallineHeight:10 isUnderline:YES underlineColor:@"F65758" underlineWidth:30 underlineHeight:3];
    [self.view addSubview:_spellSegmentedView];

    WEAKSELF;
    _spellSegmentedView.segmentClock = ^(NSInteger index){
        STRONGSELF;
        [UIView animateWithDuration:0.5 animations:^{
            CGFloat offsetX =  SCREEN_WIDTH * index;
            CGFloat offsetY = strongSelf.scrollView.contentOffset.y;
            CGPoint offset = CGPointMake(offsetX, offsetY);
            [strongSelf.scrollView setContentOffset:offset animated:YES];
        }];
    };
    [self.view addSubview:self.scrollView];
   
    for (int i = 0; i < 2; i++) {
        BSSTeacherElegantDemeanourListViewController * NewBnakChildVC = [[BSSTeacherElegantDemeanourListViewController alloc]init];;
        [self addChildViewController:NewBnakChildVC];
    }
    BSSTeacherElegantDemeanourListViewController *vc = self.childViewControllers[0];
    vc.view.frame = CGRectMake(0 *self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.scrollView addSubview:vc.view];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH *2, 0);
    UIButton *addTeacherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addTeacherBtn addTarget:self action:@selector(addTeacherClick) forControlEvents:UIControlEventTouchUpInside];
    [addTeacherBtn setTitleColor:colorCode(@"#333333") forState:0];
    [addTeacherBtn setTitle:@"+     添加老师" forState:0];
    addTeacherBtn.titleLabel.font = fontCount(16);
    [self.view addSubview:addTeacherBtn];
    [addTeacherBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView.mas_bottom);
        make.height.mas_equalTo(65);
        make.left.right.mas_equalTo(0);
    }];
   
    
}
- (void)addTeacherClick{
    BSSAddTeacherHomeViewController *vc = [[BSSAddTeacherHomeViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
/** 滚动结束后调用（代码导致） */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / self.scrollView.frame.size.width;
    
    [_spellSegmentedView changeSegmentedControlWithIndex:index];
   
    BSSTeacherElegantDemeanourListViewController *newsVc = self.childViewControllers[index];
    
    if (newsVc.view.superview) return;
    newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
    [self.scrollView addSubview:newsVc.view];
    
    
    
}
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/** 正在滚动 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__ + 40, SCREEN_WIDTH, SCREEN_HEIGHT - __kNavigationBarHeight__ - 40 - 65)];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        self.scrollView.bounces = NO;
//        self.scrollView.scrollEnabled = NO;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointZero;
      
    }
    return _scrollView;
}
@end
